How to Install OpenJDK 8 on Ubuntu 22.04 LTS 您所在的位置:网站首页 ubuntu jdk 8安装 How to Install OpenJDK 8 on Ubuntu 22.04 LTS

How to Install OpenJDK 8 on Ubuntu 22.04 LTS

2023-03-24 21:07| 来源: 网络整理| 查看: 265

Java is a general-purpose, class-based, object-oriented multipurpose programming language that is popular due to the design of having lesser implementation dependencies, meaning that the compiled Java code can be run on all platforms that support Java without the need for recompilation. Java is also fast, secure, and reliable, therefore. It is widely used for developing Java applications in laptops, data centers, game consoles, scientific supercomputers, cell phones, etc.

The tutorial will look at installing the OpenJDK version instead of the default Oracle JDK. The difference between these two is licensing. OpenJDK is an entirely free, open-source Java with a GNU General Public License, and Oracle JDK requires a commercial license under the Oracle Binary Code License Agreement. Other differences are release schedules and other factors that come into play; however, performance is the same.

In the following tutorial, you will learn how to install OpenJDK 8 LTS or better known as Java 8 LTS, on Ubuntu 22.04 LTS Jammy Jellyfish with the standard APT installation from Ubuntu’s repository along with the PPA version, which may suit some users better. The tutorial will also demonstrate how to switch Java alternative default versions.

Table of Contents

Update Ubuntu

Update your system to ensure all existing packages are up to date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade -y Install OpenJDK 8 – APT Method with Ubuntu

The first and most recommended option is to install OpenJDK using the default Ubuntu 22.04 repository. First, search to find what is available.

In your terminal, use the following command.

apt-cache search openjdk-8 | grep openjdk-8

Example output:

How to Install OpenJDK 8 on Ubuntu 22.04 LTS
Pin

As the above output states, OpenJDK 8 JDE and JRE are available to install.

To begin the installation, use the following terminal command.

Install Java 8 LTS / OpenJDK 8 LTS – JRE sudo apt-get install openjdk-8-jre -y Install Java 8 LTS / OpenJDK 8 LTS – JRE sudo apt-get install openjdk-8-jdk -y

Updates are handled with the standard apt update and upgrade commands. However, you can remove them separately or altogether if you no longer require JDK or JRE.

Example:

sudo apt-get autoremove openjdk-8-jre openjdk-8-jdk --purge

Note that this will remove any unrequited leftover dependencies and thoroughly wipe the installation and data as much as possible from your system.

Install OpenJDK 8 – APT Method with PPA

Ubuntu 22.04 repository often has the latest up-to-date and secure versions. An alternative method is to add the ppa:openjdk-r/ppa repository, which is always up to date and may see updates deployed quicker than Ubuntu’s default archives.

The PPA is untrusted and has no affiliation with the Ubuntu core team in the short term. However, it has been around for a long time and is widely used in larger communities.

Note that you do not need to remove and default OpenJDK installations. You can install this PPA directly. If any updates are available, it will prompt you to update your version from its repository.

First, add the PPA using the following command.

sudo add-apt-repository ppa:openjdk-r/ppa -y

Next, you can install JDK or JRE.

Install Java 8 LTS / OpenJDK 8 LTS – JRE sudo apt-get install openjdk-8-jre -y Install Java 8 LTS / OpenJDK 8 LTS – JRE sudo apt-get install openjdk-8-jdk -y

Users that already have the above installed just run an update to check if any newer versions are available.

sudo apt update && sudo apt upgrade

For users that no longer wish to use this PPA, you will need to remove the OpenJDK versions off your system first.

sudo apt-get remove openjdk-8-jre openjdk-8-jdk

Then remove the PPA with the –remove syntax for removal.

sudo add-apt-repository --remove ppa:openjdk-r/ppa -y Switching Alternative Java Versions

Once you installed OpenJDK 8, you may notice an alternative version number when checking using the standard java –version command. Alternatively, you may install an alternative version that you would like to switch between. This can be done with the update-alternatives –config java command.

First, check what version is enabled. The default version should always be the latest version installed; in this case, for the tutorial machine, this was the minor release version OpenJDK 18.

java --version

Example output:

How to Install OpenJDK 8 on Ubuntu 22.04 LTS
Pin

Next, list the alternative Java versions as follows.

sudo update-alternatives --config java

Example output:

How to Install OpenJDK 8 on Ubuntu 22.04 LTS
Pin

So as above, you see that Java 18 is enabled in the above example, but say you want to change to Java 8 (OpenJDK 8), you would choose the number 3 in the listing.

Example output:

How to Install OpenJDK 8 on Ubuntu 22.04 LTS
Pin

As above, OpenJDK 8 is enabled.

Test Java – Create Hello World Application

It is always handy to test installations of these kinds to confirm everything is working correctly after being installed. The easy way is to create a small test using the famous Hello World example.

First, create the Java program file as follows.

sudo nano hello.java

Next, add the following Java code to the file below.

public class hello { public static void main(String[] args) { System.out.println("hi from LinuxCapable!"); } }

Save the file CTRL+O, then exit CTRL+X.

Next, compile the code using the following terminal command javac.

javac hello.java

You may still encounter issues compiling, delete the file and use the following instead with the –release 8 .java.

javac --release 8 hello.java

Finally, run the Java code with the following command.

java hello

Example output:

How to Install OpenJDK 8 on Ubuntu 22.04 LTS
Pin Comments and Conclusion

In this tutorial, you have learned how to install Java 8 LTS on Ubuntu 22.04 LTS Jammy Jellyfish. You should now be able to run Java applications on your system that require Java 8 or, better yet, compile code.

For developers, ideally, you would like to start looking towards Java 17, which is an LTS version for your application development.

FacebookTwitterLinkedInPinterestRedditWhatsAppPocketFlipboardHacker NewsShare


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有